package cczhr.com.aoptest;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import com.anthonycr.grant.PermissionsManager;
import com.anthonycr.grant.PermissionsResultAction;

import java.util.Locale;
import java.util.Set;

public class MainActivity extends AppCompatActivity {
    private Button btn1;
    private Button btn2;
    private int data = 1;
    private BluetoothAdapter mBluetoothAdapter;

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            String action = intent.getAction();
            if(BluetoothDevice.ACTION_FOUND.equals(action)){
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                //todo处理搜索到的蓝牙设备
                Log.e("cczhr-----","搜索到蓝牙设备名称"+device.getName()+"蓝牙地址"+device.getAddress());
            }
        }
    };




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_FOUND);//每搜索到一个设备就会发送一个该广播
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);//当全部搜索完后发送该广播
        filter.setPriority(Integer.MAX_VALUE);//设置优先级
        registerReceiver(mReceiver, filter);
        PermissionsManager.getInstance().requestAllManifestPermissionsIfNecessary(this, new PermissionsResultAction() {
            @Override
            public void onGranted() {

            }

            @Override
            public void onDenied(String permission) {

            }
        });

     /*   btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("cczhr-----", "1");
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("cczhr-----", "2");

                getMessage("");
            }
        });*/
        testAOP("kkkkkkkkkkkk");

        Demo demo = new Demo(5);
        demo.setAge(666);
        getMessage(demo.getAge() + "");
        returnNumber();

        bluetool();



    }



    private void bluetool(){
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
        }
        mBluetoothAdapter.enable();

        Set<BluetoothDevice> bondedDevices = mBluetoothAdapter.getBondedDevices();
        for (BluetoothDevice value : bondedDevices) {
            Log.d("cczhr-----", "已经配对的蓝牙名称"+value.getName()+"蓝牙地址"+value.getAddress());

        }



        //如果当前在搜索，就先取消搜索
        if (mBluetoothAdapter.isDiscovering()) {
            mBluetoothAdapter.cancelDiscovery();
        }
//开启搜索
        mBluetoothAdapter.startDiscovery();

    }

    @DebugTool(resourceId = "jj")
    public void testAOP(String s) {


        Log.d("cczhr-----", "哈哈哈哈哈哈哈");
    }

    public void getMessage(String s) {
        try {
            int arr[] = new int[2];
            Log.e("cczhr-----", "年龄 " + s + arr[5]);
        } catch (Exception e) {

        }


    }


    public int returnNumber() {
        return 54;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionsManager.getInstance().notifyPermissionsChange(permissions, grantResults);
    }
}
